package com.ssslogin.auth;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-03-11.
 *
 * @author zhoufy
 */
public class ApiInterceptor implements HandlerInterceptor {

    private List<String> strings = Arrays.asList(new String[]{"/", "/index.jsp", "/login"});


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse
            response, Object handler) throws Exception {

        String requestUri = request.getRequestURI();
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute("loginInfo");

        System.out.println(requestUri);
        if (strings.contains(requestUri)) {
            if(attribute != null){
                // 已经登录 调换到主页面
                response.sendRedirect("/resume/list");
                return false;
            }
            return true;
        }

        if (attribute == null) {
            response.sendRedirect("/index.jsp");
            return false;
        }
        return true;
    }

}
